package com.duowei.diandan.db.dao;

import android.arch.lifecycle.LiveData;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import com.duowei.diandan.db.entry.Jyxmsz;

import java.util.List;

@Dao
public interface JyxmszDao {

    @Query("SELECT * FROM jyxmsz")
    List<Jyxmsz> findAll();

    @Query("SELECT * FROM jyxmsz WHERE jyxmsz.by1 NOT in(SELECT dpzsz.bm FROM dpzsz WHERE dpzsz.fxhj!='1') AND lbbm = :lbbm AND (sfqx IS NULL OR sfqx != '1') AND (gq IS NULL OR gq != '1') AND (sftc IS NULL OR sftc != '1') ORDER BY xl")
    List<Jyxmsz> findByLbbm(String lbbm);

    @Query("SELECT * FROM jyxmsz WHERE jyxmsz.by1 NOT in(SELECT dpzsz.bm FROM dpzsz WHERE dpzsz.fxhj!='1') AND lbbm = :lbbm AND (sfqx IS NULL OR sfqx != '1') AND (gq IS NULL OR gq != '1') AND (sftc IS NULL OR sftc != '1') AND by21 = '1' ORDER BY xl")
    List<Jyxmsz> findByLbbmBy21(String lbbm);

    @Query("SELECT * FROM jyxmsz WHERE lbbm = :lbbm AND (by1 IS NOT NULL AND by1 != '') GROUP BY by1")
    List<Jyxmsz> findSingleGroupByLbbm(String lbbm);

    @Query("SELECT * FROM jyxmsz WHERE lbbm = :lbbm AND (by1 IS NOT NULL AND by1 != '') AND by21 = '1' GROUP BY by1")
    List<Jyxmsz> findSingleGroupByLbbmBy21(String lbbm);

    @Query("SELECT * FROM jyxmsz WHERE xmbh = :xmbh LIMIT 1")
    Jyxmsz findByXmbh(String xmbh);

    @Query("SELECT * FROM jyxmsz WHERE xmbh = :xmbh LIMIT 1")
    LiveData<Jyxmsz> findByXmbh2(String xmbh);

    @Query("SELECT * FROM jyxmsz WHERE tm = :tm LIMIT 1")
    Jyxmsz findByTm(String tm);

    @Query("SELECT * FROM jyxmsz WHERE by1 = :by1 AND (gq IS NULL OR gq != '1') ORDER BY xl")
    List<Jyxmsz> findByBy1(String by1);

    @Query("SELECT * FROM jyxmsz WHERE by1 = :by1 AND (gq IS NULL OR gq != '1') AND by21 = '1' ORDER BY xl")
    List<Jyxmsz> findByBy1By21(String by1);

    @Query("SELECT * FROM jyxmsz WHERE by1 IN (SELECT bm FROM dpzsz WHERE bm = :dmpzsdBm)")
    LiveData<List<Jyxmsz>> findByDmpzsdBm(String dmpzsdBm);

    @Query("SELECT lbmc FROM jyxmsz WHERE xmbh = :xmbh LIMIT 1")
    String findLbmcByXmbh(String xmbh);

    @Insert
    void insert(List<Jyxmsz> list);

    @Update
    void update(Jyxmsz item);

    @Query("DELETE FROM jyxmsz")
    void deleteAll();
}
